import pandas as pd
from sklearn.feature_selection import VarianceThreshold
from scipy.stats import pearsonr
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA

def variance_demo():
    '''
    过滤低方差特征
    :return:
    '''
    data=pd.read_csv('factor_returns.csv')
    data=data.iloc[:,1:-2]
    transfer=VarianceThreshold(threshold=10)
    data_new=transfer.fit_transform(data)
    print(data.shape)
    print(data_new.shape)

    #计算两个变量相关系数
    pcc=pearsonr(data['pe_ratio'],data['pb_ratio'])
    print(pcc)

    plt.figure(figsize=(10,8),dpi=100)
    plt.scatter(data['revenue'],data['total_expense'])
    plt.show()

    pcc = pearsonr(data['revenue'], data['total_expense'])
    print(pcc)



def pca_demo():
    '''
    PCA 降维
    :return: 
    '''
    data=[[2,8,4,5],[6,3,0,8],[5,4,9,1]]
    transfer=PCA(n_components=2)
    data_new=transfer.fit_transform(data)
    print(data_new)
    transfer = PCA(n_components=0.95)
    data_new = transfer.fit_transform(data)
    print(data_new)


if __name__=="__main__":
    variance_demo()
    # pca_demo()